package com.tnovoselec.android.travelassistant.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.Util;
import com.tnovoselec.android.travelassistant.dto.ForecastFavoritesDto;
import com.tnovoselec.android.travelassistant.dto.MeteoFavoritesDto;

/**
 * Title: Android Framework - Meteo <br />
 * <br />
 * Copyright: Copyright (c) 2011 <br />
 * <br />
 * NTH Company: NTH d.o.o. <br />
 * <br />
 * 
 * MeteoAdapter displays data on {@link ViewPager} with corresponding weather
 * details.
 * 
 * @author Tomislav Novoselec
 * @version 1.0.0
 * @since 1.0.0
 * 
 * 
 */

public class MeteoAdapter extends PagerAdapter {

	private List<MeteoFavoritesDto> data;
	private Context context;

	public MeteoAdapter(Context context) {
		super();
		this.data = new ArrayList<MeteoFavoritesDto>();
		this.context = context;
	}

	public void setData(List<MeteoFavoritesDto> data) {
		if (data == null) {
			this.data.clear();
		} else {
			this.data.clear();
			this.data.addAll(data);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public void startUpdate(View container) {
		// TODO Auto-generated method stub

	}

	@Override
	public Object instantiateItem(View container, int position) {
		View root = LayoutInflater.from(context).inflate(R.layout.meteo_details, (ViewGroup) container, false);

		MeteoFavoritesDto dataDetails = data.get(position);
		ForecastFavoritesDto ffd = dataDetails.getForecast(0);
		if (ffd != null) {
			((TextView) root.findViewById(R.id.meteo_details_description)).setText(Util.conditions.get("" +dataDetails.getWeatherCode()));
			((TextView) root.findViewById(R.id.meteo_details_current_temp_value)).setText("" + dataDetails.getTemperature()
					+ context.getString(R.string.meteo_temperature_degree));
			((TextView) root.findViewById(R.id.meteo_details_current_wind_value)).setText("" + dataDetails.getWindSpeed() + " "
					+ dataDetails.getWindSpeedUnit() + " " + dataDetails.getWindDirection());
			((TextView) root.findViewById(R.id.meteo_details_current_humidity_value)).setText("" + dataDetails.getHumidity());
			((TextView) root.findViewById(R.id.meteo_details_temp_high)).setText(String.format(context.getString(R.string.meteo_temp_high),
					ffd.getTemperatureHigh()));
			((TextView) root.findViewById(R.id.meteo_details_temp_low)).setText(String.format(context.getString(R.string.meteo_temp_low),
					ffd.getTemperatureLow()));
			((ViewPager) container).addView(root, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		} else {
		//	Log.e("kita od metea", dataDetails.getCityId());
		}
		return root;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager) container).removeView((View) object);

	}

	@Override
	public void finishUpdate(View container) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return object == view;
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
		// TODO Auto-generated method stub

	}

	@Override
	public CharSequence getPageTitle(int position) {
		return data.get(position).getTitle();
	}

}
